package com.javabasic.crawler;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/1/16 15:31
 * @Version 1.0
 **/
public class CrawlerTest {

    static OkHttpClient client = new OkHttpClient();

    static String run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .addHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows 7)")
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) {
        try {
            String[] strs = new String[]{"https://www.aliexpress.com/item/33001269207.html","https://www.aliexpress.com/item/4000235525675.html"};
            String response = run(strs[1]);
//            System.out.println(response);

            Elements script = Jsoup.parse(response).getElementsByTag("script");
            System.out.println(script.size());
            Element first = script.get(12);

            String data = first.data();
            System.out.println(data);

            int tradeCount = data.indexOf("\"tradeCount\"");
            int i = data.indexOf(",", tradeCount);
            String substring = data.substring(tradeCount, i);
            // 抓取到销售额
            System.out.println(substring);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
